from datetime import datetime
# 调用datetime中的datetime类

from django.db import models
from django.contrib.auth.models import AbstractUser

# Create your models here.
# 自定义userprofile表覆盖默认的user表


GENDER_CHOICES = (
    ("male", "男"),
    ("female", "女")
)


class BaseModel(models.Model):
    # users在最底层所以放置基类
    add_time = models.DateTimeField(default=datetime.now, verbose_name="添加时间")

    class Meta:
        # 不生成表只用来继承
        abstract = True


class UserProfile(AbstractUser):
    # 继承user类
    nick_name = models.CharField(max_length=50, verbose_name="昵称", default="")
    # null=True, blank=True ，字段可以为空，第二种 default=""默认为空
    birthday = models.DateField(verbose_name="生日", null=True, blank=True)
    gender = models.CharField(verbose_name="性别", choices=GENDER_CHOICES, max_length=6)
    # choices 限制用户选择的范围，只能接受的是元组。
    address = models.CharField(max_length=100, verbose_name="地址", default="")
    mobile = models.CharField(max_length=11, verbose_name="手机号")
    # mobile 为唯一数据，设置unique约束，超级用户创建时，会出现两个空手机号，会报错, 所以先去除unique约束，在注册登录时进行约束
    image = models.ImageField(upload_to="head_image/%Y/%m", default="default.jpg", verbose_name="头像")
    # upload_to 上传路径，后期在media文件夹下创建head_image/年/月文件夹，在media的根路径下保存保人头像default.jpg

    class Meta:
        verbose_name = "用户信息"
        verbose_name_plural = verbose_name
        db_table = "tb_user"

    def unread_nums(self):
        """未读消息数量"""
        return self.usermessage_set.filter(has_read=False).count()

    def __str__(self):
        if self.nick_name:
            return self.nick_name
        else:
            return self.username
